package com.zzuli.practice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sun.org.apache.xpath.internal.operations.Or;
import com.zzuli.practice.entity.Order;
import com.zzuli.practice.entity.Repairman;
import com.zzuli.practice.entity.Result;
import com.zzuli.practice.entity.User;
import com.zzuli.practice.service.impl.LoginService;
import com.zzuli.practice.service.impl.OrderService;
import com.zzuli.practice.service.impl.WorkerService;
import com.zzuli.practice.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/order")
@EnableTransactionManagement // 开启事务
public class OrderController implements Constant {

    @Autowired
    private OrderService orderService;

    @Autowired
    private WorkerService workerService;

    @Autowired
    private LoginService loginService;

    @RequestMapping({"/pageorder/{pageIndex}", "/pageorder"})
    public Result getPageWorkerList(@PathVariable(value = "pageIndex", required = false) Integer pageIndex){
        Result result = new Result();
        try{
            IPage<Order> iPage = orderService.pageOrderList(pageIndex);
            if (iPage != null && iPage.getSize() > 0){
                result.setStatusCode(SUCCESS);
                result.setMessage("success");
                result.setData(iPage);
            } else {
                result.setStatusCode(UNFIND);
                result.setMessage("没有数据");
            }
        } catch (Exception e){
            e.printStackTrace();
        }

        System.out.println("查询订单信息");

        return result;
    }

    @RequestMapping(value = "/insertorder", method = RequestMethod.POST)
    public Result addOrder(String username, String address, String type){

        Result result = new Result();

        // 判断数据合法性
        if (StringUtils.isBlank(username)){
            result.setStatusCode(FAILURE);
            result.setMessage("您还未登录");
            return result;
        }
        if (StringUtils.isBlank(address)){
            result.setStatusCode(FAILURE);
            result.setMessage("地址不能为空");
            return result;
        }
        if (StringUtils.isBlank(type)){
            result.setStatusCode(FAILURE);
            result.setMessage("设备类型不能为空");
            return result;
        }

        // 添加数据
        Order order = new Order();
        order.setUsername(username);
        order.setAddress(address);
        order.setType(type);
        order.setStatus(0);

        try{
            int i = orderService.insertOrder(order);
            if (i == 1){
                result.setStatusCode(SUCCESS);
                result.setMessage("添加成功");
            } else {
                result.setStatusCode(FAILURE);
                result.setMessage("添加失败");
            }
        } catch (Exception e){
            e.printStackTrace();
            result.setStatusCode(FAILURE);
            result.setMessage("系统异常");
        }

        return result;
    }

    // 设置事务传播行为
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @RequestMapping(value = "/assign", method = RequestMethod.POST)
    public Result assignOrder(Integer id, Integer orderid) throws Exception{

        Result result = new Result();

        // 空值处理
        if (id == null || orderid == null){
            result.setStatusCode(FAILURE);
            result.setMessage("数据异常");
            return result;
        }

        // 查询维修员
        Repairman repairman = workerService.selectById(id);
        if (repairman == null){
            result.setStatusCode(FAILURE);
            result.setMessage("该维修员不存在");
            return result;
        }
        // 维修员正在维修其他设备
        if (repairman.getStatus() == 1){
            result.setStatusCode(FAILURE);
            result.setMessage("该维修员正在忙碌中");
            return result;
        }

        // 修改订单以及维修员的状态
        int i = workerService.updateForStatusById(id, BUSY);
        int i1 = orderService.updateForStatusById(orderid, BUSY, repairman.getName(), repairman.getId());

        if (i == 1 && i1 == 1){
            result.setStatusCode(SUCCESS);
            result.setMessage("派遣成功");
            return result;
        } else {
            result.setStatusCode(FAILURE);
            result.setMessage("系统异常");
            return result;
        }

    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @RequestMapping(value = "/overorder/{orderId}", method = RequestMethod.POST)
    public Result overOrder(@PathVariable("orderId") Integer id, String username){
        Result result = new Result();

        // 空值处理
        if (id == null){
            result.setStatusCode(FAILURE);
            result.setMessage("订单号为空");
            return result;
        }
        if (StringUtils.isBlank(username)){
            result.setStatusCode(FAILURE);
            result.setMessage("您还未登录");
            return result;
        }

        // 查询用户权限
        User user = loginService.selectByUserName(username);
        if (user == null){
            result.setStatusCode(FAILURE);
            result.setMessage("该用户不存在");
            return result;
        }
        if (user.getType() == 0){
            result.setStatusCode(FAILURE);
            result.setMessage("您没有权限");
            return result;
        }

        // 查询订单，获取维修员id
        Order order = orderService.selectById(id);
        if (order == null){
            result.setStatusCode(FAILURE);
            result.setMessage("该订单不存在");
            return result;
        }

        // 修改维修员状态
        int i = workerService.updateForStatusById(order.getRepairmanid(), FREE);
        // 修改订单状态
        int i1 = orderService.updateForStatusById(id, OVER, order.getRepairman(), order.getRepairmanid());
        // 逻辑删除订单
        int i2 = orderService.deleteOrderById(id);

        if (i == 1 && i1 == 1 & i2 == 1){
            result.setStatusCode(SUCCESS);
            result.setMessage("操作成功");
            return result;
        } else {
            result.setStatusCode(FAILURE);
            result.setMessage("系统异常");
            return result;
        }

    }

}
